if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].GetAnswerForProcess') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].GetAnswerForProcess
GO

/***************************************************************************
Name:					GetAnswerForProcess

Purpose:				This Stored Procedure retrieves answers 
						the new or existing ProtocolProcessId.
PARAMETERS
Name					Description					
 -------------			-----------------------------------------------------------
@protocolProcessId int	Process Id for the Protocol

-----------
Returns
Name of the Questionnaire
***************************************************************************/

CREATE PROCEDURE dbo.GetAnswerForProcess
(  
	@protocolProcessId int
)  
AS   
BEGIN  

	SET NOCOUNT ON	
	
	SELECT ans.Name, ans.Value
	FROM ProtocolProcess pp
	INNER JOIN ProtocolVersion pv
	ON pv.ProtocolProcessId = pp.id
	INNER JOIN ProtocolVersion_QuestionnaireAnswer_Map pvm
	ON pv.Id = pvm.ProtocolVersionId
	INNER JOIN QuestionnaireAnswer qa
	ON qa.Id = pvm.QuestionnaireAnswerId
	INNER JOIN Answer ans
	ON ans.QuestionnaireAnswerId = pvm.QuestionnaireAnswerId
	WHERE pp.Id = @protocolProcessId
	
END

GO

